<!--
 * @Author: xuwejie 1529315455@qq.com
 * @Date: 2023-10-25 14:11:20
 * @LastEditors: xuwejie 1529315455@qq.com
 * @LastEditTime: 2023-10-26 11:59:02
 * @FilePath: \old-supply-shop_vue\components\storeMap\index.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="content">
    <!-- <map
      id="container"
      :markers="markers"
      :longitude="location.lng"
      :latitude="location.lat"
      show-location
    ></map> -->
  </div>
</template>

<script>
import { $https } from "@/static/js/request.js";
export default {
  props: {},
  data() {
    return {
      markers: [], // 点
      location: {
        // 位置
        lng: "",
        lat: "",
      },
    };
  },
  mounted() {
    this.getPower();
  },
  methods: {
    // 获取是否授权位置信息
    getPower() {
      uni.getSetting({
        success: (res) => {
          if (
            res.authSetting &&
            res.authSetting.hasOwnProperty("scope.userLocation")
          ) {
            if (res.authSetting["scope.userLocation"]) {
              this.getLocation();
            } else {
              uni.showModal({
                title: "提示",
                content:
                  "请重新授权获取你的地理位置，否则部分功能将无法使用.\r\n提示:点击小程序右上角的三个点在设置中修改授权",
                success: (res) => {
                  if (res.confirm) {
                    uni.openSetting({
                      success: () => this.getLocation(),
                    });
                  } else {
                    this.getCovidData();
                  }
                },
              });
            }
          } else {
            this.getCityInfo();
          }
        },
      });
    },
    getCityInfo() {
      uni.authorize({
        scope: "scope.userFuzzyLocation",
        success: () => {
          this.getLocation();
        },
        fail: (error) => {
          uni.showModal({
            title: "提示",
            content: "若点击不授权，将无法使用位置功能",
            cancelText: "不授权",
            cancelColor: "#999",
            confirmText: "授权",
            confirmColor: "#f94218",
            success(res) {
              if (res.confirm) {
                // 选择弹框内授权
                uni.openSetting({
                  success(res) {
                    this.getLocation();
                  },
                });
              } else if (res.cancel) {
                // 选择弹框内 不授权
                console.log("用户点击不授权");
              }
            },
          });
        },
      });
    },
    // 获取位置
    async getLocation() {
      try {
        const result = await uni.getLocation({
          type: "gcj02",
        });
        const locationObj = result[1];
        let lng = locationObj.longitude;
        let lat = locationObj.latitude;
        this.location = {
          lng,
          lat,
        };
        let option = {
          lng,
          lat,
          page: 1,
          size: 100000,
          name: "",
        };
        $https(
          "GET",
          "getStoreList",
          option,
          (res) => {
            if (res.data.errcode == 0) {
              let list = res.data.data.list;
              // 传递列表和位置信息
              this.$emit("getList", { list, location: this.location });
              list.forEach((item) => {
                this.markers.push({
                  id: item.id,
                  latitude: item.lat,
                  longitude: item.lng,
                  width: 30, // 设置标记的宽度
                  height: 45, // 设置标记的高度
                });
              });
            } else {
              this.$u.toast(res.data.errmsg);
            }
          },
          true
        );
      } catch (error) {
        console.log("获取位置信息失败: " + JSON.stringify(error));
      }
    },
  },
};
</script>

<style lang='scss' scoped>
.content {
  map {
    width: 100%;
    height: 600rpx;
  }
}
</style>